<?php
/*
* 应用模块安装卸载
*/
namespace system\platform\controller;

class App{

   
    function index(){
       
    } 

    // 列表 安装过
    function installed_list(){
        //$sql="select * from " . tablename('sys_module')  . "  order by id";
        //$module_installed = $GLOBALS['db']->query($sql);
        require view('default/app/installed_list.tpl.php');
    }

    // 卸载
    function installed_do_uninstall(){
        $app_name = $_GET['app_name'];

        // 读取配置
        $config =  require _APP_PATH_ . "/application/$app_name/manifest.php";

        // 执行卸载SQL 
        if( isset( $config['sql']['uninstall'] ) && trim( $config['sql']['uninstall'] ) != '' ){
            if(file_exists(_APP_PATH_ ."/application/$app_name/" . $config['sql']['uninstall'])){
                require _APP_PATH_ ."/application/$app_name/" . $config['sql']['uninstall'] ;
            }else{
                message('卸载失败！未发现SQL卸载文件。',url(['platform','app','installed_list']));
            }
        }

        // 删除菜单
        $sql ="delete from  " . tablename('sys_module') . " where identifie = '$app_name' ;";
        $sql .="delete from  " . tablename('sys_module_menu') . " where module_identifie = '$app_name' ;";
        $res = $GLOBALS['db']->query($sql);


        if($res){
            message('卸载成功',url(['platform','app','installed_list']));
        }else{
            message('卸载失败',url(['platform','app','installed_list']));
        }
    }

    // 列表 未安装
    function not_installed_list(){
        $sql="select * from " . tablename('sys_module') . " ";
        $rs_module = $GLOBALS['db']->query($sql);
        $dir_module = [];
        foreach($rs_module as $row){
            $dir_module[]=$row['identifie'];
        }

        $dir_app_tmp = scandir( _APP_PATH_ . '/application/' );
        $dir_app = [];
        // 比对目录
        foreach($dir_app_tmp as $k => $v){
            if(in_array( $v , $dir_module) 
                || $v=='.' || $v=='..' 
                || is_dir(_APP_PATH_ . '/application/' . $v)==false){
                unset($dir_app_tmp[$k]);
            }
        }
        // 加载未安装应用的清单文件
        foreach($dir_app_tmp as $v){
            if(is_file(_APP_PATH_ . "/application/$v/manifest.php")){
                $tmp =  require _APP_PATH_ . "/application/$v/manifest.php";
                $dir_app[] = $tmp['application'];
            }
        }

        require view('default/app/not_installed_list.tpl.php');
    }

    // 安装
    function not_installed_do_install(){
        $app_name = $_GET['app_name'];
		
		// 判断是否已经安装
        $sql ="select * from  " . tablename('sys_module') . " where identifie = '$app_name' ;";
        $ret = $GLOBALS['db']->query($sql);
		if($ret->fetch()){
            message('安装失败！应用模块已经安装。',url(['platform','app','not_installed_list']));			
		}
	
        // 读取配置
        $config =  require _APP_PATH_ . "/application/$app_name/manifest.php";

        // 执行安装SQL
        if( isset( $config['sql']['install'] ) && trim( $config['sql']['install'] ) != '' ){
            if(file_exists(_APP_PATH_ ."/application/$app_name/" . $config['sql']['install'])){
                require _APP_PATH_ ."/application/$app_name/" . $config['sql']['install'] ; // config/sql_install.php
            }else{
                message('安装失败！未发现SQL安装文件。',url(['platform','app','not_installed_list']));
            }
        }
        
        
        $data = [
            'identifie' => $config['application']['identifie'] ,
            'name' => $config['application']['name'] ,
            'version' => $config['application']['version'] ,
            'author' => $config['application']['author'] ,
            'description' => $config['application']['description'] ,
            'type' => $config['application']['type'] ,
            'icon' => $config['application']['icon'] ,
        ];
        $res = pdo_insert('sys_module',$data);

        foreach($config['menu'] as $vo){
            $data = [
                'module_identifie' => $config['application']['identifie'] ,
                'module_name' => $config['application']['name'] ,
                'name' => $vo['name'] ,
                'icon' => isset($vo['icon']) ? $vo['icon']:'' ,
                'controller' => isset($vo['controller']) ? $vo['controller']:'' ,
                'action' => isset($vo['action']) ? $vo['action']:'' ,
                'm_id'  => isset($vo['m_id']) ? $vo['m_id']:0,
                'm_pid' => isset($vo['m_pid'])?$vo['m_pid']:0 ,
            ];
            $res = pdo_insert('sys_module_menu',$data);
        }

        if($res > 0 ){
            message('安装成功',url(['platform','app','not_installed_list']));
        }else{
            message('安装失败',url(['platform','app','not_installed_list']));
        }
        
    }



}